import scala.collection.mutable.ListBuffer

case class Song(title: String, artist: String, album: String)

object PlayMode extends Enumeration {
  type PlayMode = Value
  val Loop, Shuffle, RepeatOne, RepeatAll = Value
}

class MusicPlayer() {
  import PlayMode._

  private var songs: ListBuffer[Song] = ListBuffer.empty
  private var currentIndex: Int = -1
  private var currentMode: PlayMode = Loop

  def addSong(song: Song): Unit = {
    songs += song
  }

  def removeSong(index: Int): Unit = {
    if (index >= 0 && index < songs.length) {
      songs.remove(index)
      if (songs.isEmpty) {
        stop()
      } else if (currentIndex == index) {
        if (currentIndex >= songs.length) {
          currentIndex = songs.length - 1
        }
        play()
      } else if (currentIndex > index) {
        currentIndex -= 1
      }
    }
  }

  def play(): Unit = {
    if (songs.nonEmpty) {
      currentMode match {
        case Loop =>
          if (currentIndex < 0 || currentIndex >= songs.length) {
            currentIndex = 0
          }
        case Shuffle =>
          currentIndex = util.Random.nextInt(songs.length)
        case RepeatOne =>
          if (currentIndex < 0 || currentIndex >= songs.length) {
            currentIndex = 0
          }
        case RepeatAll =>
          if (currentIndex < 0) {
            currentIndex = 0
          } else if (currentIndex >= songs.length) {
            currentIndex = 0
          }
      }
      println(s"Now playing: ${songs(currentIndex).title} by ${songs(currentIndex).artist}")
    }
  }

  def pause(): Unit = {
    println("Paused")
  }

  def stop(): Unit = {
    currentIndex = -1
    println("Stopped")
  }

  def next(): Unit = {
    currentIndex += 1
    play()
  }

  def previous(): Unit = {
    currentIndex -= 1
    play()
  }

  def switchMode(mode: PlayMode): Unit = {
    currentMode = mode
  }

}
